package com.lightsaber.trade.as.service.reta;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.dao.reta.OrderDao;
import com.lightsaber.trade.as.dao.reta.OrderItemDao;
import com.lightsaber.trade.as.dao.reta.OrderTemplateDao;
import com.lightsaber.trade.as.entity.reta.Order;
import com.lightsaber.trade.as.entity.reta.OrderItem;
import com.lightsaber.trade.as.entity.reta.OrderTemplate;
import com.lightsaber.trade.as.entity.reta.OrderTemplateItem;
import com.lightsaber.trade.core.common.util.CurrentUser;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class OrderManager extends BaseServiceImpl<Order, String> {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @Autowired
    private OrderTemplateDao orderTemplateDao;

    @Override
    protected BaseDao<Order, String> getDao() {
        return orderDao;
    }

    public void findPage(Order Order, Page<Order> page) {
        orderDao.findPage(Order, page);
    }

    public Order saveOrder(Order order) {

        if (StringUtils.isBlank(order.getId())) {
            order.setOrderTemplate(null);
            order.setOrganization(CurrentUser.getOrg());

            this.save(order);

            return order;
        } else {

            Order old = this.get(order.getId());

            BeanUtils.copyProperties(order, old, new String[] { BaseEntity.ID, BaseEntity.VERSION,
                    AuditedObject.CREATED_BY, AuditedObject.CREATED_DATE, "totalPrice",
                    "orderTemplate", "organization" });

            this.save(old);
            return old;
        }
    }

    public Order useOrderTemplate(String orderId, String orderTemplateId) {
        OrderTemplate orderTemplate = orderTemplateDao.get(orderTemplateId);
        Order order = null;

        if (StringUtils.isBlank(orderId)) {

            order = new Order();
            order.setName(orderTemplate.getName());
            order.setDescription(orderTemplate.getDescription());
            order.setOrderTemplate(orderTemplate);
            order.setTotalPrice(orderTemplate.getTotalPrice());
            order.setOrganization(CurrentUser.getOrg());

            this.save(order);

        } else {

            order = this.get(orderId);

            order.setOrderTemplate(orderTemplate);
            order.setTotalPrice(orderTemplate.getTotalPrice());

            List<OrderItem> orderItemList = order.getOrderItemList();
            order.setOrderItemList(null);
            for (OrderItem orderItem : orderItemList) {
                orderItemDao.delete(orderItem);
            }

            this.save(order);

        }

        this.generateOrderItemUseTemplate(orderTemplate, order);

        return order;
    }

    private void generateOrderItemUseTemplate(OrderTemplate orderTemplate, Order order) {

        for (OrderTemplateItem orderTemplateItem : orderTemplate.getOrderTemplateItemList()) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrder(order);

            orderItem.setProduct(orderTemplateItem.getProduct());
            orderItem.setPrice(orderTemplateItem.getPrice());
            orderItem.setNumber(orderTemplateItem.getNumber());

            orderItemDao.save(orderItem);
        }
    }

    public void onItemChanged(String OrderId) {
        Order Order = this.get(OrderId);

        Double totalPrice = Double.valueOf(0d);
        for (OrderItem OrderItem : Order.getOrderItemList()) {
            totalPrice += (OrderItem.getNumber() * OrderItem.getPrice());
        }

        Order.setTotalPrice(totalPrice);
        orderDao.save(Order);

    }

}
